package com.famdoc.task;

import com.famdoc.Pojo.entity.Visit;
import com.famdoc.Service.VisitService;
import com.famdoc.Utils.MqConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 延时队列消费者
 */
@Component
@Slf4j
public class DelayQueueConsumer {

    @Autowired
    private VisitService visitService;

    /**
     * 随访记录状态修改消费者
     * @param message
     */
    @RabbitListener(queues = MqConstants.DELAYED_VISIT_QUEUE_NAME)
    public void receiveDelayedQueue(Message message){
        String msg = new String(message.getBody());
        int visitId = Integer.parseInt(msg);
        Visit visit = visitService.getVisitById(visitId);
        if(visit != null){
            //如果随访不为空，且随访为预约状态
            if(visit.getVisitSign() == -1){
                //将随访状态修改为已逾期
                visitService.updateVisitStatusDue(visitId);
            }
        }
        log.info("当前时间：{},收到延时队列的消息：{}", new Date(), msg);
    }
}